﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FurnitureSystem
{
    public partial class frmThemSP : Form
    {
        SqlConnection conn;
        SqlDataAdapter da;
        DataSet ds;
        private int Typeid;

        public frmThemSP()
        {
            InitializeComponent();
        }
        public DataSet LoadCombo()
        {
            conn = connect.Getconect();
            conn.Open();
            string sql = "select * from Type";
            SqlCommand cmd = new SqlCommand(sql, conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "type");
            return ds;
            
        }
       
     
        private void frmThemSP_Load(object sender, EventArgs e)
        {
            cBLoaiHang.DataSource = LoadCombo().Tables[0];
            cBLoaiHang.DisplayMember = LoadCombo().Tables[0].Columns[1].ToString();
            cBLoaiHang.ValueMember = LoadCombo().Tables[0].Columns[0].ToString();
        }

        

        private void BtnThoat_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void BtnThem_Click(object sender, EventArgs e)
        {
            if (txtMaSanPham.Text == "" || txtTenSanPham.Text == "" || txtSoluong.Text == "" || txtGiaBan.Text == "")
            {
                MessageBox.Show("Vui lòng điền đẩy đủ thông tin");
            }
            else
            {
                this.Typeid = Int32.Parse(this.cBLoaiHang.SelectedValue.ToString());
                conn = connect.Getconect();
                conn.Open();
                string sql = "insert into Product values ('" + txtMaSanPham.Text + "','" + txtTenSanPham.Text + "','" + Int32.Parse(txtGiaBan.Text) + "','" + Int32.Parse(txtSoluong.Text) + "','" + Typeid + "')";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader dr = cmd.ExecuteReader();
                conn.Close();
                MessageBox.Show("Đã thêm sản phẩm vào CSDL");
                txtGiaBan.Text = "";
                txtMaSanPham.Text = "";
                txtSoluong.Text = "";
                txtTenSanPham.Text = "";
                txtMaSanPham.Focus();
            }
        }

       

        
    }
}
